package com.banshuibao.bsb_android.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.AddressModel;
import com.banshuibao.bsb_android.model.ExpertTreeModel;
import com.banshuibao.bsb_android.model.RecorderContentModel;
import com.banshuibao.bsb_android.model.RegionModel;
import com.banshuibao.bsb_android.util.ClearDirUtil;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.MyEditText;
import com.banshuibao.bsb_android.util.SelectPopupWindowUtil;
import com.banshuibao.bsb_android.util.XToast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class RecorderContentActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout tax_type, topic_type, trad_type, areaId_type, areaId_tax_type, next;

    private LinearLayout all_tag, portion_tag;

    private MyEditText title, content, price;

    private TextView type_tv;

    private String uuid, recorder_title, recorder_content, recorder_price;

    private String tax, taxUuid, topic, topicUuid, trade, tradeUuid, areaId, areaIdUuid, areaId_tax, areaId_taxUuid, type;
    private TextView tax_tv, topic_tv, trade_tv, areaId_tv, areaId_tax_tv;

    private Context context;
    private String name;
    private ExpertTreeModel expertTreeModel;
    private SelectPopupWindowUtil selectPopupWindowUtil;
    private PopupWindow popupWindow;
    private RadioButton all_typ, portion_type;
    private List<ExpertTreeModel.DataBean.ChildrenBean> tradeList;
    private List<ExpertTreeModel.DataBean.ChildrenBean> taxList;
    private List<ExpertTreeModel.DataBean.ChildrenBean> topicList;
    private List<AddressModel> list;
    private RelativeLayout price_layout;
    private String role;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recorder_content);
        context = this;
        role = GetLoginInfo.getLoginInfoInstance(this).readUser().getRole();
        initView();
        list = new ArrayList<>();
        getTreeData();
    }

    public void getTreeData() {
        /**
         * 行业税种专题数据
         */
        HttpUtil.doPost(Constants.Url.EXPERT_TREE, null, new HttpResponse(this, ExpertTreeModel.class) {
            @Override
            public void onResult(Object obj) {
                expertTreeModel = (ExpertTreeModel) obj;
                List<ExpertTreeModel.DataBean> data = expertTreeModel.getData();
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).getName().equals("行业")) {
                        tradeList = data.get(i).getChildren();
                    } else if (data.get(i).getName().equals("税种")) {
                        taxList = data.get(i).getChildren();
                    } else if (data.get(i).getName().equals("专题")) {
                        topicList = data.get(i).getChildren();
                    }
                }
            }
        });

        /**
         * 地区数据
         */
        HttpUtil.doPost(Constants.Url.EXPERT_ADDRESS, null, new HttpResponse(this, RegionModel.class) {
            @Override
            public void onResult(Object obj) {
                RegionModel regionModel = (RegionModel) obj;
                TreeMap<String, String> json = regionModel.getData();
                setAddress(json);
            }
        });
    }

    public void initView() {
        setTitleText("新增录音");
        price_layout = findViewById(R.id.price_layout);
        if (role.equals("2") || role.equals("3")) {
            price_layout.setVisibility(View.VISIBLE);
        } else {
            price_layout.setVisibility(View.GONE);
        }
        trad_type = findViewById(R.id.trade_type);
        tax_type = findViewById(R.id.tax_type);
        topic_type = findViewById(R.id.topic_type);
        areaId_type = findViewById(R.id.areaId_type);
        areaId_tax_type = findViewById(R.id.areaId_tax_type);
        all_tag = findViewById(R.id.all_tag);
        portion_tag = findViewById(R.id.portion_tag);
        next = findViewById(R.id.next);
        title = findViewById(R.id.title);
        content = findViewById(R.id.content_et);
        price = findViewById(R.id.price_et);
        price.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int i, int i1, Spanned dest, int i2, int i3) {
                if (source.equals(".") && dest.toString().length() == 0) {
                    return "0.";
                }
                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int length = dest.toString().substring(index).length();
                    if (length == 3) {
                        return "";
                    }
                }
                return null;
            }
        }});
        tax_tv = findViewById(R.id.tax);
        trade_tv = findViewById(R.id.trade);
        topic_tv = findViewById(R.id.topic);
        areaId_tv = findViewById(R.id.areaId);
        areaId_tax_tv = findViewById(R.id.areaId_tax);
        type_tv = findViewById(R.id.type);
        trad_type.setOnClickListener(this);
        tax_type.setOnClickListener(this);
        topic_type.setOnClickListener(this);
        areaId_type.setOnClickListener(this);
        areaId_tax_type.setOnClickListener(this);
        next.setOnClickListener(this);
        selectPopupWindowUtil = new SelectPopupWindowUtil(this);
        selectPopupWindowUtil.setContent(new SelectPopupWindowUtil.ItemOnClickListener() {
            @Override
            public void getContent(String name, String uuid) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
                if (name.equals("无")) {
                    name = "";
                }
                switch (type) {
                    case "trade":
                        trade = name;
                        tradeUuid = uuid;
                        trade_tv.setText(trade);
                        break;
                    case "tax":
                        tax = name;
                        taxUuid = uuid;
                        tax_tv.setText(tax);
                        break;
                    case "topic":
                        topic = name;
                        topicUuid = uuid;
                        topic_tv.setText(topic);
                        break;
                    case "areaId":
                        areaId = name;
                        areaIdUuid = uuid;
                        areaId_tv.setText(areaId);
                        break;
                    case "areaId_tax":
                        areaId_tax = name;
                        areaId_taxUuid = uuid;
                        areaId_tax_tv.setText(areaId_tax);
                        break;
                }

            }
        });

        all_typ = findViewById(R.id.title1);
        portion_type = findViewById(R.id.title2);
        all_typ.setOnClickListener(this);
        portion_type.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.trade_type:
                type = "trade";
                if (tradeList != null && tradeList.size() > 0) {
                    showPopupWindows(view, tradeList, null, "tree");
                }
                break;
            case R.id.tax_type:
                type = "tax";
                if (taxList != null && taxList.size() > 0) {
                    showPopupWindows(view, taxList, null, "tree");
                }

                break;
            case R.id.topic_type:
                type = "topic";
                if (topicList != null && topicList.size() > 0) {
                    showPopupWindows(view, topicList, null, "tree");
                }

                break;
            case R.id.areaId_type:
                type = "areaId";
                if (list != null && list.size() > 0) {
                    showPopupWindows(view, null, list, "");
                }

                break;
            case R.id.areaId_tax_type:
                type = "areaId_tax";
                if (taxList != null && taxList.size() > 0) {
                    showPopupWindows(view, taxList, null, "tree");
                }

                break;
            case R.id.next:
                getData();
                break;
            case R.id.title1:
                clearPortion();
                all_tag.setVisibility(View.VISIBLE);
                portion_tag.setVisibility(View.GONE);
                break;
            case R.id.title2:
                clearAll();
                all_tag.setVisibility(View.GONE);
                portion_tag.setVisibility(View.VISIBLE);
                break;
        }
    }

    public void showPopupWindows(View view, List<ExpertTreeModel.DataBean.ChildrenBean> children, List<AddressModel> list, String tag) {
        popupWindow = selectPopupWindowUtil.getPopupWindow(children, list, tag);
        popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
//                if (data != null) {
//                    AddressModel addressModel = (AddressModel) data.getSerializableExtra("type");
//                  //  uuid = addressModel.getUuid();
//                  //  name = addressModel.getName();
//                 //   type_tv.setText(name);
//                //    tax_type = addressModel.getParameter_type();
//                } else {
//                    XToast.showToast(RecorderContentActivity.this, "获取数据失败");
//                }
                break;
            case 1001:
                finish();
                break;
        }

    }

    public void getData() {
        recorder_title = title.getText().toString().trim();
        recorder_content = content.getText().toString().trim();
        recorder_price = price.getText().toString().trim();
        if (TextUtils.isEmpty(recorder_title)) {
            XToast.showToast(context, "请输入标题");
            return;
        } else if (TextUtils.isEmpty(recorder_content)) {
            XToast.showToast(context, "请输入录音文本");
            return;
        } else if (role.equals("2") || role.equals("3")) {
            if (TextUtils.isEmpty(recorder_price)) {
                XToast.showToast(context, "请输入问题价格");
                return;
            } else {
                double d = Double.valueOf(recorder_price);
                if (d == 0 || d < 1) {
                    XToast.showToast(context, "问题的价格要大于1元");
                    return;
                }
            }
        }
        if (TextUtils.isEmpty(tradeUuid) && TextUtils.isEmpty(taxUuid) && TextUtils.isEmpty(topicUuid) && TextUtils.isEmpty(areaIdUuid) && TextUtils.isEmpty(areaId_taxUuid)) {
            XToast.showToast(RecorderContentActivity.this, "请选择语音问题类型");
            return;
        } else if (TextUtils.isEmpty(tradeUuid) && TextUtils.isEmpty(taxUuid) && TextUtils.isEmpty(topicUuid)) {
            if (TextUtils.isEmpty(areaIdUuid) && TextUtils.isEmpty(areaId_taxUuid)) {
                XToast.showToast(RecorderContentActivity.this, "请选择语音问题类型");
                return;
            } else if (!TextUtils.isEmpty(areaIdUuid) && TextUtils.isEmpty(areaId_taxUuid)) {
                XToast.showToast(RecorderContentActivity.this, "请选择地区的税种");
                return;
            } else if (TextUtils.isEmpty(areaIdUuid) && !TextUtils.isEmpty(areaId_taxUuid)) {
                XToast.showToast(RecorderContentActivity.this, "请选择地区");
                return;
            }
        }
        RecorderContentModel model = new RecorderContentModel();
        setAll(model);
        model.setRecorder_content(recorder_content);
        model.setRecorder_price(recorder_price);
        model.setRecorder_title(recorder_title);
        IntentManager.goRecorderVideoActivity(RecorderContentActivity.this, model);
    }


    public void setAddress(TreeMap<String, String> hashMap) {
        //遍历集合得到业务id+名称
        for (TreeMap.Entry<String, String> entry : hashMap.entrySet()) {
            list.add(new AddressModel(entry.getValue(), entry.getKey(), ""));
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ClearDirUtil.clearAll(new File(Constants.LocalFile.DIR));
        getSharedPreferences("record", MODE_PRIVATE).edit().clear().commit();
    }

    public void clearAll() {
        tax_tv.setText("");
        taxUuid = "";
        tax = "";
        trade_tv.setText("");
        trade = "";
        tradeUuid = "";
        topic_tv.setText("");
        topic = "";
        topicUuid = "";
    }

    public void clearPortion() {
        areaId_tax_tv.setText("");
        areaId_tax = "";
        areaId_taxUuid = "";
        areaId = "";
        areaIdUuid = "";
        areaId_tv.setText("");
    }

    public void setAll(RecorderContentModel model) {
        if (!TextUtils.isEmpty(tradeUuid)) {
            model.setTrade_name(trade);
            model.setTrade_uuid(tradeUuid);
            model.setTrade_type("trade");
        }
        if (!TextUtils.isEmpty(taxUuid)) {
            model.setTax_name(tax);
            model.setTax_uuid(taxUuid);
            model.setTax_type("tax");
        }
        if (!TextUtils.isEmpty(topicUuid)) {
            model.setTopic_name(topic);
            model.setTopic_uuid(topicUuid);
            model.setTopic_type("topic");
        }
        if (!TextUtils.isEmpty(areaIdUuid)) {
            model.setAreaId_name(areaId);
            model.setAreaId_uuid(areaIdUuid);
            model.setAreaId_type("areaId");
        }
        if (!TextUtils.isEmpty(areaId_taxUuid)) {
            model.setAreaId_tax_name(areaId_tax);
            model.setAreaId_tax_uuid(areaId_taxUuid);
            model.setAreaId_tax_type("areaId_tax");
        }

    }

}
